package com.cuitrip.business.bill;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.cuitrip.apiservice.OrderBusiness;
import com.cuitrip.apiservice.model.ApiManager;
import com.cuitrip.service.R;
import com.cuitrip.util.PlatformUtil;
import com.lab.adapter.IAdapterData;
import com.lab.component.list.adapter.CTRecyclerAdapter;
import com.lab.component.list.viewholder.CTRecycleViewHolder;
import com.lab.network.model.CtApiResponse;
import com.lab.network.model.IProxyCallback;
import com.lab.network.proxy.ApiProxy;
import com.lab.network.proxy.ApiProxyBuilder;
import com.lab.network.proxy.ApiProxyCallBack;
import com.lab.utils.ImageHelper;
import com.lab.utils.LogHelper;
import com.lab.utils.MessageUtils;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by MiJack on 2015/11/2.
 */
public class RatingTodayFragment extends Fragment {

    RecyclerView recyclerView;
    View root;
    ProgressBar progressBar;
    RatingAdapter ratingAdapter;
    ApiProxy apiProxy;
    private IProxyCallback callback=new ApiProxyCallBack<List<Rate>>(){
        @Override
        public boolean onSuccess(List<Rate> data, CtApiResponse response) {
            LogHelper.d("TAG",data.toString());
            progressBar.setVisibility(View.GONE);
            if (data instanceof List) {
                if (data.size() == 0) {
                    Toast.makeText(getActivity(), R.string.feedback_no_data, Toast.LENGTH_SHORT).show();
                }
                setRating(data);
            }
            return super.onSuccess(data, response);
        }
        @Override
        public boolean onFaild(CtApiResponse response) {
            progressBar.setVisibility(View.GONE);
            String msg;
            if (response != null && !TextUtils.isEmpty(response.msg)) {
                msg = response.msg;
            } else {
                msg = PlatformUtil.getInstance().getString(R.string.feedback_data_err);
            }
            MessageUtils.showToast(msg);
            return super.onFaild(response);
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.fragment_rating, container, false);
        recyclerView = (RecyclerView) root.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        ratingAdapter = new RatingAdapter();
        recyclerView.setAdapter(ratingAdapter);
        progressBar = (ProgressBar) root.findViewById(R.id.progressBar);
        apiProxy= ApiProxyBuilder.getBuilder().addApiCallBack(ApiManager.GET_RATES,callback).build();
        OrderBusiness.getRates(apiProxy,0,20);
        return root;
    }

    public void setRating(List<Rate> list){
        //排序
        Collections.sort(list, new Comparator<Rate>() {
            @Override
            public int compare(Rate lhs, Rate rhs) {
                int l = getIndex(lhs.currency.toUpperCase());
                int r = getIndex(rhs.currency.toUpperCase());
                if (l == r) {
                    return 0;
                } else if (l > r) {
                    return 1;
                } else
                    return -1;
            }

            private int getIndex(String key) {
                switch (key) {
                    case "USD" :return 0;
                    case "EUR" :return 1;
                    case "CNY" :return 2;
                    case "JPY" :return 3;
                    case "KRW" :return 4;
                    case "TWD" :return 5;
                }
                return 9;
            }
        });

        ratingAdapter.setList(list);
    }

    private static class RatingAdapter extends CTRecyclerAdapter<RatingHolder> {
        private List<Rate> ratingModels;

        @Override
        public RatingHolder newViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rating, parent, false);
            RatingHolder holder = new RatingHolder(view);
            return holder;
        }

        @Override
        public int getItemCount() {
            return ratingModels == null ? 0 : ratingModels.size();
        }

        @Override
        public void onBindViewHolder(RatingHolder holder, int position) {
            Rate model=ratingModels.get(position);
            holder.textView.setText(model.currency+" "+model.rate);
            ImageHelper.displayPersonImage(model.icon, holder.imageView, null);
        }

        public void setList(List<Rate> list) {
            this.ratingModels = list;
            this.notifyDataSetChanged();
        }
    }

    static class RatingHolder extends CTRecycleViewHolder {
        public ImageView imageView;
        public TextView textView;

        public RatingHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.image);
            textView = (TextView) itemView.findViewById(R.id.textView);

        }

        @Override
        public void render(IAdapterData iAdapterData, int position) {

        }
    }

}
